package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class MainShow extends Sprite
	{
		public var container:Sprite = null;
		public var file:FileIcon;
		
		public function MainShow()
		{
			
			super();
			if (stage)
			{
				init(null);
			}
			else
			{
				this.addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		
		private function init(e:Event):void
		{
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addChild(new Bitmap(new BitmapData(stage.stageWidth, stage.stageHeight)));
			if (container == null) container = new Sprite;
			addChild(container);
			var w:int = 50;
			
			var back:TextField = ViewSet.make_txt(stage.stageWidth - w, 0, "关闭", w, 20, 0xff0000, 16);
			addChild(back);
			back.background = true;
			back.border = true;
			back.addEventListener(MouseEvent.CLICK, clicked);
			
			var download:TextField = ViewSet.make_txt(stage.stageWidth - w * 2, 0, "下载", w, 20, 0xff0000, 16);
			addChild(download);
			download.background = true;
			download.border = true;
			download.addEventListener(MouseEvent.CLICK, clicked);
		
		}
		
		public function addObj(obj:DisplayObject):void
		{
			//if (container)
			//ViewSet.removes(container);
			if (container == null)
				container = new Sprite();
			container.addChild(obj);
			visible = true;
		}
		
		private function clicked(e:MouseEvent):void
		{
			switch (e.target.name)
			{
			case "关闭": 
				visible = false;
				//if (container)
				//ViewSet.removes(container);
				dispatchEvent(new Event("close"));
				break;
			case "下载": 
				if (file && file.fdata)
				{
					trace(e.target.name);
					var fileRef:FileReference = new FileReference();
					fileRef.save(file.fdata, file.filename);
				}
				//visible = false;
				//if (container)
				//ViewSet.removes(container);
				
				dispatchEvent(new Event("下载"));
				break;
			}
		}
	
	}

}